﻿'-------------------------------------------------------------------------------
'Developer: Ivan Tamurejo
'-------------------------------------------------------------------------------
'This module contains the main implementation of the application. 
'Enables full compilacinones using a listing of order of compilation. 
'Also allows you to perform a simple compilation of a component.
'
'Requires the 'VB6Builds' class.
'-------------------------------------------------------------------------------
Imports System.IO
Imports Builds

Public Module Main

    Sub main()

        Dim oVB6Builds As VB6Builds = Nothing
        Dim sArguments As String = UCase(Command())
        Dim iPos As Integer = 0
        Dim sArgument1 As String = ""
        Dim sArgument2 As String = ""
        Dim sArgument3 As String = ""
        Dim sArgument4 As String = ""
        Dim sArgument5 As String = ""
        Dim sAux As String = ""
        Dim bWriteTime As Boolean = False
        Dim sOutDir As String = ""
        Dim bSecondDir As Boolean = False

        'Init without error.
        Environment.ExitCode = 0

        Try

            oVB6Builds = New VB6Builds()

            iPos = InStr(sArguments, " ")
            If iPos = 0 Then
                sArgument1 = sArguments
            Else
                sArgument1 = Trim(Mid(sArguments, 1, iPos - 1))
            End If

            If sArgument1 = "/START" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then

                        sAux = ""
                        For i As Integer = 0 To sArgument2.Length - 1

                            If sArgument2.Chars(i) <> "/" Then
                                sAux += sArgument2.Chars(i)
                            Else
                                sArgument3 = Mid(sArgument2, i + 1)
                                Exit For
                            End If

                        Next
                        sArgument2 = Trim(sAux)

                        If sArgument3 <> "" Then

                            iPos = InStr(sArgument3, " ")
                            If iPos <> 0 Then
                                sArgument4 = Trim(Mid(sArgument3, iPos + 1))
                                sArgument3 = Trim(Mid(sArgument3, 1, iPos - 1))

                                If sArgument3 = "/COPYBUILDS" Then

                                    sAux = ""
                                    For i As Integer = 0 To sArgument4.Length - 1

                                        If sArgument4.Chars(i) <> "/" Then
                                            sAux += sArgument4.Chars(i)
                                        Else
                                            sArgument5 = Mid(sArgument4, i + 1)
                                            Exit For
                                        End If

                                    Next
                                    sOutDir = Trim(sAux)

                                    If sOutDir <> "" Then
                                        If sArgument5 <> "" Then
                                            If sArgument5 = "/WRITETIME" Then
                                                bWriteTime = True
                                            Else
                                                Help()
                                                Exit Sub
                                            End If
                                        End If
                                    Else
                                        Help()
                                        Exit Sub
                                    End If

                                ElseIf sArgument3 = "/WRITETIME" Then
                                    bWriteTime = True
                                Else
                                    Help()
                                    Exit Sub
                                End If
                            Else
                                Help()
                                Exit Sub
                            End If
                        End If

                        'If we have indicated argument, the compilation is done the project indicated in
                        'the input argument.
                        oVB6Builds.StartBuilds(sArgument2, sOutDir, bWriteTime)

                    Else
                        Help()
                    End If
                Else
                    Help()
                End If

                Exit Sub
            End If

            'Clean builds directory
            If sArgument1 = "/CLEAN" Then
                oVB6Builds.CleanBuildsDirectory()
                Exit Sub
            End If

            'Clean files logs that have been created today.
            If sArgument1 = "/CLEANLOGS" Then
                oVB6Builds.CleanBuildsLogs()
                Exit Sub
            End If

            'This argument allows editing the date and time of the file indicated.
            If sArgument1 = "/WRITETIMEFILE" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then
                        oVB6Builds.SetWriteLastTimeFile(sArgument2)
                    Else
                        Help()
                    End If
                Else
                    Help()
                End If
                Exit Sub
            End If

            'This argument allows editing the date and time of all files in the directory indicated.
            If sArgument1 = "/WRITETIME" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then
                        oVB6Builds.SetWriteLastTimeDir(sArgument2)
                    Else
                        Help()
                    End If
                Else
                    Help()
                End If
                Exit Sub
            End If

            'This argument allows editing the date and time of all files and subfolders of the directory indicated.
            If sArgument1 = "/WRITETIMERECURSIVE" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then
                        oVB6Builds.SetWriteLastTimeDirRecursive(sArgument2)
                    Else
                        Help()
                    End If
                Else
                    Help()
                End If
                Exit Sub
            End If

            'This argument allows you to copy files of the source directory to the target directory.
            If sArgument1 = "/COPY" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then

                        sAux = ""
                        For i As Integer = 0 To sArgument2.Length - 1

                            If Not (sArgument2.Chars(i) = ":" Or (sArgument2.Chars(i) = "\" And sArgument2.Chars(i + 1) = "\")) Then
                                sAux += sArgument2.Chars(i)
                            Else
                                If Not bSecondDir Then
                                    bSecondDir = True
                                    sAux += sArgument2.Chars(i)
                                Else

                                    sAux = Left(sAux, sAux.Length - 1)
                                    sArgument3 = Trim(Mid(sArgument2, i))
                                    Exit For
                                End If
                            End If

                        Next
                        sArgument2 = Trim(sAux)

                        If sArgument3 <> "" Then
                            oVB6Builds.Copy(sArgument2, sArgument3)
                        Else
                            Help()
                        End If
                    Else
                        Help()
                    End If
                Else
                    Help()
                End If
                Exit Sub
            End If


            'This argument allows you to copy compiled files to the directory indicated.
            If sArgument1 = "/COPYBUILDS" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then
                        oVB6Builds.CopyContentsBuildsDirTo(sArgument2)
                    Else
                        Help()
                    End If
                Else
                    Help()
                End If
                Exit Sub
            End If

            'This argument allows you to sort the specified project references
            If sArgument1 = "/SORTREFERENCES" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then
                        oVB6Builds.SortReferences(sArgument2)
                    Else
                        Help()
                    End If
                Else
                    Help()
                End If
                Exit Sub
            End If

            'This argument allows notify by e-mail the results of compilations
            If sArgument1 = "/NOTIFY" Then
                iPos = InStr(sArguments, " ")
                If iPos <> 0 Then
                    sArgument2 = Trim(Mid(sArguments, iPos + 1))
                    If sArgument2 <> "" Then
                        oVB6Builds.Notify(sArgument2)
                    Else
                        Help()
                    End If
                Else
                    Help()
                End If
                Exit Sub
            End If

            Help()

        Catch ex As Exception
            'Exit width error.
            Environment.ExitCode = 1

            'Write in Builds Log
            If Not oVB6Builds Is Nothing Then
                Builds.Logs.WriteLog(oVB6Builds.BuildsDirectory, ex.Message, eTypeLog.Robot)
            Else
                WriteConsole(ex.Message)
            End If
        Finally
            If Not oVB6Builds Is Nothing Then
                oVB6Builds.Dispose()
            End If
        End Try

    End Sub

    Private Sub Help()

        'Exit width error.
        Environment.ExitCode = 1

        'Show help.
        WriteConsole(vbCrLf & vbCrLf & _
                     "Uses:" & vbCrLf & _
                     " 1)RobotBuilds /Start <Project> [/CopyBuilds <Directory> [/WriteTime]]" & vbCrLf & _
                     " 2)RobotBuilds /Start <OrderList> [/CopyBuilds <Directory> [/WriteTime]]" & vbCrLf & _
                     " 3)RobotBuilds /Clean" & vbCrLf & _
                     " 4)RobotBuilds /CleanLogs" & vbCrLf & _
                     " 5)RobotBuilds /WriteTimeFile <File>" & vbCrLf & _
                     " 6)RobotBuilds /WriteTime <Directory>" & vbCrLf & _
                     " 7)RobotBuilds /WritetimeRecursive <Directory>" & vbCrLf & _
                     " 8)RobotBuilds /Copy <Source directory> <Target directory>" & vbCrLf & _
                     " 9)RobotBuilds /CopyBuilds <Directory>" & vbCrLf & _
                     "10)RobotBuilds /SortReferences <Project>" & vbCrLf & _
                     "11)RobotBuilds /Notify <E-emails separated by ';'>" & vbCrLf & vbCrLf & _
                     "/Start <Project file path '. vbp'>" & vbCrLf & _
                     "      Info: Starts the compilation of the specified project." & vbCrLf & vbCrLf & _
                     "/Start <Order of compilation file path>" & vbCrLf & _
                     "      Info: Starts the compilation of all projects included " & vbCrLf & _
                     "            in the compilation order file." & vbCrLf & vbCrLf & _
                     "/Clean" & vbCrLf & _
                     "      Info: Cleans the builds directory, deleting components " & vbCrLf & _
                     "            and unregisters them." & vbCrLf & vbCrLf & _
                     "/CleanLogs" & vbCrLf & _
                     "      Info: Clean files logs that have been created today." & vbCrLf & vbCrLf & _
                     "/WriteTimeFile <File>" & vbCrLf & _
                     "      Info: Modifies the date and time of the file indicated." & vbCrLf & vbCrLf & _
                     "/WriteTime <Diretory>" & vbCrLf & _
                     "      Info: Modifies the date and time of all files in the " & vbCrLf & _
                     "            directory indicated." & vbCrLf & vbCrLf & _
                     "/WriteTimeRecursive <Directory>" & vbCrLf & _
                     "      Info: Modifies the date and time of all files specified " & vbCrLf & _
                     "            directory and subdirectories." & vbCrLf & vbCrLf & _
                     "/Copy <Source directory> <Target directory>" & vbCrLf & _
                     "      Info: Allows you to copy subdirectories and files to the " & vbCrLf & _
                     "            target directory." & vbCrLf & vbCrLf & _
                     "/CopyBuilds <Directory>" & vbCrLf & _
                     "      Info: Allows you to copy compiled files to the directory " & vbCrLf & _
                     "            indicated." & vbCrLf & _
                     "/SortReferences <Project>" & vbCrLf & _
                     "      Info: Allows you to sort the specified project references." & vbCrLf & _
                     "/Notify <E-emails separated by ';'>" & vbCrLf & _
                     "      Info: Allows notify by e-mail the results of compilations.")

    End Sub

End Module
